goog.provide('czk.ui.DateBox');

goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.i18n.DateTimeFormat');
goog.require('goog.i18n.DateTimeParse');
goog.require('goog.ui.DatePicker');
goog.require('goog.ui.DatePicker.Events');
goog.require('goog.ui.InputDatePicker');
goog.require('czk.ui.ZKComponent');

/**
 * @constructor
 * @extends {czk.ui.ZKComponent}
 */
czk.ui.DateBox = function() {
  goog.base(this);
  goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en_GB;
  this.PATTERN = 'dd.MM.yyyy';
};
goog.inherits(czk.ui.DateBox, czk.ui.ZKComponent);

czk.ui.DateBox.prototype.PATTERN = null;
czk.ui.DateBox.prototype.enableHoliday = null;
czk.ui.DateBox.prototype.onChangeEvent = null;
czk.ui.DateBox.prototype.gdp = null;
czk.ui.DateBox.prototype.idp = null;

czk.ui.DateBox.prototype.initialize =
    function(name, parameter, portlet, children) {
      goog.base(this, 'initialize', name, parameter, portlet, children);
    };

czk.ui.DateBox.prototype.createDom = function() {
  var dh = this.getDomHelper();
  var el = dh.createDom('input', {
    'className': this.className + ' datePicker',
    'name': this.name,
    'value': this.value,
    'type': 'text'
  });

  this.setElementInternal(el);
  this.decorateHelper(el);
};

czk.ui.DateBox.prototype.decorateHelper = function(element) {
  var formatter = new goog.i18n.DateTimeFormat(this.PATTERN);
  var parser = new goog.i18n.DateTimeParse(this.PATTERN);  
  this.idp = new goog.ui.InputDatePicker(formatter, parser);    
  this.idp.decorate(element);

};

/** @inheritDoc */
czk.ui.DateBox.prototype.enterDocument =
    function() {
      goog.base(this, 'enterDocument');
      this.getHandler().listen(this.getElement(),
          goog.ui.Component.EventType.CHANGE, this.change, false, this);

    };

czk.ui.DateBox.prototype.change = function() {
  var data = '{"value":"' + this.getElement()['value'] + '"}';
  this.portlet.sendXhr(this, 'onChange', data);

};

czk.ui.DateBox.prototype.handleZKResponseCommand = function(command) {
  goog.base(this, 'handleZKResponseCommand', command);
  this.getElement()['value'] = this.value;
};

czk.ui.DateBox.create = function() {
  return new czk.ui.DateBox();
};
